﻿using UnityEngine;
using System.Collections;

public class PlayerPolice : MonoBehaviour 
{
	
	private Animator anim;

	public Collider2D box;

	public bool Avistado;

	public float walkVelocity;
	bool directionWalk = true;
	float invertion = -1;

	public float runVelocity;
	public float timeRun;
	float timeRunOriginal;

	float velocity;

	public float up_x;
	public float up_y;
	public int andarPolicial;
	public float distanciaElevador;
	public float distanciaEscada;

	public bool UpStairsRight;
	public bool UpStairsLeft;

	public GameObject player;
	public GameObject gameOver;

	public float policialInicioX;
	public float policialInicioY;

	GameObject escada;
	public GameObject escada1;
	public GameObject escada2;
	public GameObject escada3;

	GameObject elevador;
	public GameObject elevador1;
	public GameObject elevador2;
	public GameObject elevador3;


	// Use this for initialization
	void Start () 
	{
		anim = gameObject.GetComponent<Animator>();
		box =  gameObject.GetComponent<BoxCollider2D>();
		velocity = walkVelocity;
		timeRunOriginal = timeRun;
		policialInicioX = gameObject.transform.position.x;
		policialInicioY = gameObject.transform.position.y;
	}
	
	// Update is called once per frame
	void Update () 
	{	distanciaElevador = gameObject.transform.position.x - elevador.transform.position.x;
		distanciaElevador = gameObject.transform.position.x - escada.transform.position.x;
		if (!Avistado) 
		{
			timeRun -= Time.deltaTime;
			velocity = walkVelocity;


			if (timeRun < 0 && directionWalk)
			{
				directionWalk = false;
				timeRun = timeRunOriginal;
			}
			if (timeRun < 0 && !directionWalk) 
			{
				directionWalk = true;
				timeRun = timeRunOriginal;
			}

			if (directionWalk) 
			{
				gameObject.transform.Translate (velocity * Time.deltaTime, 0, 0);
				gameObject.transform.localScale = new Vector3 (0.46f, 0.46f, 1);
			}
			if (!directionWalk) 
			{
				gameObject.transform.Translate (-velocity * Time.deltaTime, 0, 0);
				gameObject.transform.localScale = new Vector3 (-0.46f, 0.46f, 1);
			}
		}
		if (Avistado && !UpStairsRight && !UpStairsLeft)
		{
			velocity = runVelocity;
			if(player.GetComponent<Player>().andar == andarPolicial)
			{
				if(player.transform.position.x > gameObject.transform.position.x)
				{
					gameObject.transform.Translate (velocity * Time.deltaTime, 0, 0);
					gameObject.transform.localScale = new Vector3 (0.46f, 0.46f, 1);
				}
				else
				{
					gameObject.transform.Translate (-velocity * Time.deltaTime, 0, 0);
					gameObject.transform.localScale = new Vector3 (-0.46f, 0.46f, 1);
				}
			}
			else
			{
				if(gameObject.transform.position.x - elevador.transform.position.x > gameObject.transform.position.x - escada.transform.position.x)
				{
					if(andarPolicial == 1 || andarPolicial == 3)
					{
						gameObject.transform.Translate (velocity * Time.deltaTime, 0, 0);
						gameObject.transform.localScale = new Vector3 (0.46f, 0.46f, 1);
					}
					if(andarPolicial == 2)
					{
						gameObject.transform.Translate (-velocity * Time.deltaTime, 0, 0);
						gameObject.transform.localScale = new Vector3 (-0.46f, 0.46f, 1);
					}

				}
				if(gameObject.transform.position.x - elevador.transform.position.x < gameObject.transform.position.x - escada.transform.position.x)
				{
					if(andarPolicial == 1 || andarPolicial == 3)
					{
						gameObject.transform.Translate (-velocity * Time.deltaTime, 0, 0);
						gameObject.transform.localScale = new Vector3 (-0.46f, 0.46f, 1);
					}
					if(andarPolicial == 2)
					{
						gameObject.transform.Translate (velocity * Time.deltaTime, 0, 0);
						gameObject.transform.localScale = new Vector3 (0.46f, 0.46f, 1);
					}
					/*if(andarPolicial == 2)
					{
						gameObject.transform.Translate (velocity * Time.deltaTime, 0, 0);
						gameObject.transform.localScale = new Vector3 (0.46f, 0.46f, 1);
					}*/
				}
			}
		}
		//aqui estão as variaveis que fazem o player subir as escadas, com suas respectivas direções descritas
		if(UpStairsRight)
		{
			transform.Translate(up_x * Time.deltaTime, up_y * Time.deltaTime, 0);
			gameObject.rigidbody2D.gravityScale = 0;
			velocity = 0;
			box.isTrigger = true;
			gameOver.GetComponent<BoxCollider2D>().enabled = false;
		}
		if(UpStairsLeft)
		{
			transform.Translate((-1*up_x) * Time.deltaTime, up_y * Time.deltaTime, 0);
			gameObject.rigidbody2D.gravityScale = 0;
			box.isTrigger = true;
			velocity = 0;
			gameOver.GetComponent<BoxCollider2D>().enabled = false;
		}
		
		//desliga-se a gravidade quando o player sobe as escadas, nesse teste vejo se ele está subindo alguma escada, e caso não, ligo a fisica do jogo
		if(!UpStairsRight && !UpStairsLeft)
		{
			gameObject.rigidbody2D.gravityScale = 1;
			box.isTrigger = false;
			gameOver.GetComponent<BoxCollider2D>().enabled = true;
		}

	}
	
	

	void OnCollisionEnter2D(Collision2D other)
	{
		if(other.gameObject.tag == "Clip Stairs Right")
		{
			UpStairsRight = true;
		}
		if(other.gameObject.tag == "Clip Stairs Left")
		{
			UpStairsLeft = true;
	
		}

		/*if(other.gameObject.tag == "Player")
		{

			Debug.Log("OK");

			anim.SetTrigger("VerLadraoDireita");

			velocity = runVelocity;

			//gameObject.a.SetTrigger("VerLadraoDireita");

			//other.gameObject.collider2D.isTrigger = true;
			
			gameObject.collider2D.isTrigger = true;
			Avistado = true;

			
			//BoxCollider2D.isTrigger = true;
			
		}*/
	

	}
	void OnTriggerEnter2D(Collider2D other)
	{
		if(other.gameObject.tag == "Stop Clip Stairs" )
		{
			UpStairsRight = false;
			UpStairsLeft = false;
		}
		if (other.gameObject.tag == "Player") 
		{
			Avistado = true;
			anim.SetTrigger("VerLadraoDireita");
		}
		if (other.gameObject.tag == "Ametista") 
		{
			if(other.gameObject.GetComponent<SpriteRenderer>().enabled == false)
			{
				Avistado = true;
				anim.SetTrigger("VerLadraoDireita");
			}
		}
		if (other.gameObject.tag == "1") 
		{
			andarPolicial = 1;
			escada = escada1;
			elevador = elevador1;
		}
		if (other.gameObject.tag == "2") 
		{
			andarPolicial = 2;
			escada = escada2;
			elevador = elevador2;
		}
		if (other.gameObject.tag == "3") 
		{
			andarPolicial = 3;
			escada = escada3;
			elevador = elevador3;

		}
		if (other.gameObject.tag == "4") 
		{
			andarPolicial = 4;
		}
	}
}



